package com.hawthorn.admin.service.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hawthorn.admin.model.po.SysUserTmpPO;
import com.hawthorn.admin.repository.SysUserTmpMapper;
import com.hawthorn.platform.utils.common.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>todo 导入excel保存至临时表类</p>
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2021/12/22 10:24 AM
 * @version v1.0.1
 * @copyright: Copyright (c) 2021 andyten
 */
@Service
public class ImportExlSaveTmp
{
  @Autowired
  SysUserTmpMapper sysUserTmpMapper;

  /**
   * <p>todo excel导入用户临时表</p>
   * @param: list
   * @param: userAccount
   * @param: bizPk
   * @return: void
   * @author: andy.ten@tom.com | xukaixing@hotmail.com
   * @date: 2021/12/22 9:31 PM
   * @version: 1.0.2
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2021/12/22    andy.ten        v1.0.1             init
   * 2022/05/30    andy.ten        v1.0.2             fix bug
   */
  public void saveSysUserTmp(List<Map<Integer, Object>> list, String userAccount, Long bizPk, Boolean delFlag, Map<String, Object> params)
  {
    // todo 先删除临时表数据
    if (delFlag == true)
    {
      LambdaQueryWrapper<SysUserTmpPO> qw = Wrappers.lambdaQuery();
      qw.eq(SysUserTmpPO::getCreateBy, userAccount);
      sysUserTmpMapper.delete(qw);
    }
    // todo 批量导入临时表
    List<SysUserTmpPO> batchPo = new ArrayList<>();
    AtomicInteger rowNums = new AtomicInteger(1);
    list.forEach(map ->
    {
      SysUserTmpPO sysUserTmpPO = new SysUserTmpPO();
      // fixme: 1.0.2 解决map获取null问题 2022-05-30
      sysUserTmpPO.setRowNums(rowNums.getAndIncrement());
      sysUserTmpPO.setAccount(MyUtil.getValue(map.get(0)));
      sysUserTmpPO.setUserName(MyUtil.getValue(map.get(1)));
      sysUserTmpPO.setCreateBy(userAccount);

      batchPo.add(sysUserTmpPO);
    });
    sysUserTmpMapper.saveBatch(batchPo, SysUserTmpPO.class);
  }
}
